<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入计划名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="计划状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择计划状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker v-model="queryParams.startDate" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker v-model="queryParams.endDate" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="计划类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择计划类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_INSPECTION_PLAN_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="计划人员" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入计划人员" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['device:inspection-plan:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['device:inspection-plan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="计划名称" align="center" prop="name" />
      <el-table-column label="计划状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.startDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.endDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划周期" align="center" prop="cycle" />
      <el-table-column label="计划类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DEVICE_INSPECTION_PLAN_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="提醒时间(时)" align="center" prop="reminderTime" />
      <el-table-column label="预计用时(时)" align="center" prop="estimatedTime" />
      <el-table-column label="计划人员" align="center" prop="nickname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['device:inspection-plan:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCreateJob(scope.row)"
                     v-hasPermi="['device:inspection-plan:createJob']">创建任务</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['device:inspection-plan:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入计划名称" class="widthSet" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择计划类型" class="widthSet" >
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_INSPECTION_PLAN_TYPE)"
                           :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker clearable v-model="form.startDate" type="date" value-format="timestamp" placeholder="选择开始日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker clearable v-model="form.endDate" type="date" value-format="timestamp" placeholder="选择结束日期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="计划周期" prop="cycle">
          <el-input v-model="form.cycle" placeholder="请输入计划周期" style="width: 590px">
            <template slot="append">
              <el-button type="primary" @click="handleShowCron">
                生成表达式
                <i class="el-icon-time el-icon--right"></i>
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提醒时间" prop="reminderTime">
              <el-input v-model="form.reminderTime" placeholder="请输入提醒时间(时)" class="widthSet"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计用时" prop="estimatedTime">
              <el-input v-model="form.estimatedTime" placeholder="请输入预计用时(时)"  class="widthSet" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="计划人员" prop="userId">
          <selectSingleUser  v-model="form.userId" :isCheck="true"></selectSingleUser>
        </el-form-item>
        <el-form-item label="计划状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                      :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :rows="6" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选择设备" >
          <select-multiple-device v-model="deviceList" ></select-multiple-device>
        </el-form-item>
        <el-table :data="tableData" sortable style="width: 100%">
          <el-table-column prop="id" label="设备序号" align="center">
          </el-table-column>
          <el-table-column prop="code" sortable label="设备编号" align="center">
          </el-table-column>
          <el-table-column prop="name" sortable label="设备名称" align="center">
          </el-table-column>
          <el-table-column>
            <template v-slot="scope">
              <el-button type="danger" plain icon="el-icon-plus" size="mini" v-if="form.type === 1" @click="handleEntry(scope.row)">检查项</el-button>
              <el-button type="danger" plain icon="el-icon-plus" size="mini" v-if="form.type === 2" @click="handleMaintenanceEntry(scope.row)">保养项</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="Cron表达式生成器" :visible.sync="openCron" append-to-body class="scrollbar" destroy-on-close>
      <crontab @hide="openCron=false" @fill="crontabFill" :expression="expression"></crontab>
    </el-dialog>

    <!--检查项设置-->
    <el-dialog :title="entry.title" :visible.sync="entry.open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="entryForm" :model="entry.device"  label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备序号" prop="id">
              <el-input v-model="entry.device.id" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="code">
              <el-input v-model="entry.device.code" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="entry.device.name" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--操作按钮-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="chooseEntry">选择</el-button>
          </el-col>
        </el-row>
        <!--检查项按钮-->
        <el-table v-loading="entry.loading" :data="entry.list">
          <el-table-column label="序号" align="center" prop="entryId" />
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="编码" align="center" prop="code" />
          <el-table-column label="状态" align="center" prop="status">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="描述" align="center" prop="content" />
          <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleEntryDelete(scope.row)"
                         v-hasPermi="['device:inspection-plan:entryDelete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="entry.total > 0" :total="entry.total" :page.sync="entry.queryParams.pageNo" :limit.sync="entry.queryParams.pageSize"
                    :page-sizes.sync="entry.queryParams.pageSizes"
                    @pagination="getEntryList"/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="entrySubmitForm">确 定</el-button>
        <el-button @click="entryCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--选择检查项-->
    <select-entry :openSelectEntry.sync="openSelectEntry" :device-id.sync="entry.device.id"
                   @getEntryList="getEntryList"></select-entry>

    <!--保养项-->
    <el-dialog :title="maintenanceEntry.title" :visible.sync="maintenanceEntry.open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="entryForm" :model="maintenanceEntry.device"  label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备序号" prop="id">
              <el-input v-model="maintenanceEntry.device.id" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="code">
              <el-input v-model="maintenanceEntry.device.code" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="maintenanceEntry.device.name" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--操作按钮-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="chooseMaintenanceEntry">选择</el-button>
          </el-col>
        </el-row>
        <!--检查项按钮-->
        <el-table v-loading="maintenanceEntry.loading" :data="maintenanceEntry.list">
          <el-table-column label="项目编号" align="center" prop="entryId" />
          <el-table-column label="项目名称" align="center" prop="name" />
          <el-table-column label="状态" align="center" prop="status">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="保养类型" align="center" prop="type">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.DEVICE_MAINTENANCE_PLAN_TYPE" :value="scope.row.type" />
            </template>
          </el-table-column>
          <el-table-column label="工时(时)" align="center" prop="workHours" />
          <el-table-column label="保养内容" align="center" prop="content" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleMaintenanceEntryDelete(scope.row)"
                         v-hasPermi="['device:maintenance-device-entry:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="maintenanceEntry.total > 0" :total="maintenanceEntry.total" :page.sync="maintenanceEntry.queryParams.pageNo" :limit.sync="maintenanceEntry.queryParams.pageSize"
                    :page-sizes.sync="maintenanceEntry.queryParams.pageSizes"
                    @pagination="getMaintenanceEntryList()"/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="maintenanceSubmitForm">确 定</el-button>
        <el-button @click="maintenanceEntryCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--选择保养项-->
    <select-maintenance-entry :openMaintenanceSelectEntry.sync="openMaintenanceSelectEntry" :device-id.sync="maintenanceEntry.device.id"
                              @getMaintenanceEntryList="getMaintenanceEntryList" ></select-maintenance-entry>
  </div>
</template>

<script>
import {
  createInspectionPlan,
  deleteInspectionPlan,
  exportInspectionPlanExcel,
  getInspectionPlan,
  getInspectionPlanPage,
  updateInspectionPlan
} from "@/api/device/inspectionPlan";
import {createInspectionPlanJob} from "@/api/device/inspectionPlanJob";
import {deleteInspectionDeviceEntry, getInspectionDeviceEntryPage} from '@/api/device/inspectionDeviceEntry'
import Crontab from "@/components/Crontab/index.vue";
import SelectSingleUser from "@/components/SelectSingleUser/index.vue";
import SelectMultipleDevice from "@/components/SelectMultipleDevice/index.vue";
import SelectEntry from "@/components/SelectEntry/index.vue";
import SelectMaintenanceEntry from "@/components/SelectMaintenanceEntry/index.vue";
import {getMaintenanceDeviceEntryPage,deleteMaintenanceDeviceEntry} from "@/api/device/maintenanceDeviceEntry";

export default {
  name: "InspectionPlan",
  components: {
    Crontab,SelectSingleUser,SelectMultipleDevice,SelectEntry,SelectMaintenanceEntry
  },
  data() {
    return {
      // 是否展示检查项选择窗口
      openSelectEntry: false,
      // 是否展示保养项选择窗口
      openMaintenanceSelectEntry: false,
      // 保养项
      maintenanceEntry:{
        title: "保养项",
        open: false,
        device:{
          id : "",
          name: "",
          code: ""
        },
        loading: true,
        list: [],
        total: 0,
        queryParams: {
          pageNo: 1,
          pageSize: 5,
          pageSizes: [5,10,20],
          deviceId: null
        },
      },
      // 检查项
      entry:{
        title: "检查项",
        open: false,
        device:{
          id : "",
          name: "",
          code: ""
        },
        loading: true,
        list: [],
        total: 0,
        queryParams: {
          pageNo: 1,
          pageSize: 5,
          pageSizes: [5,10,20],
          deviceId: null
        },
      },
      // 设备展示列表
      tableData:[],
      // 多选设备列表
      deviceList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检计划列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        status: null,
        startDate: [],
        endDate: [],
        type: null,
        userId: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "计划名称不能为空", trigger: "blur" }],
        status: [{ required: true, message: "计划状态不能为空", trigger: "blur" }],
        startDate: [{ required: true, message: "开始日期不能为空", trigger: "blur" }],
        endDate: [{ required: true, message: "结束日期不能为空", trigger: "blur" }],
        cycle: [{ required: true, message: "计划周期不能为空", trigger: "blur" }],
        type: [{ required: true, message: "计划类型不能为空", trigger: "change" }],
        reminderTime: [{ required: true, message: "提醒时间不能为空", trigger: "blur" },
          {required: true,pattern:/^((0{1}\.\d+)|([1-9]\d*\.{1}\d+)|([1-9]+\d*))$/ ,message: "提醒时间必须大于0", trigger: "blur"}],
        estimatedTime: [{ required: true, message: "预计用时不能为空", trigger: "blur" },
          {required: true,pattern:/^((0{1}\.\d+)|([1-9]\d*\.{1}\d+)|([1-9]+\d*))$/ ,message: "预计用时必须大于0", trigger: "blur"}],
        userId: [{ required: true, message: "计划人员不能为空", trigger: "blur" }],
      },
      // 是否显示Cron表达式弹出层
      openCron: false,
      // 传入的表达式
      expression: "",
    };
  },
  created() {
    this.getList();
  },
  watch: {
    deviceList: {
      handler(newVal, oldVal) {
        this.tableData = []
        if (newVal.length > 0) {
          newVal.forEach(item => {
            this.tableData.push({"id": item.id, "code": item.code, "name": item.name})
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    /*保养计划*/
    // 保养项按钮操作
    handleMaintenanceEntry(data){
      this.maintenanceEntryReset()
      this.maintenanceEntry.device.id = data.id
      this.maintenanceEntry.device.name = data.name
      this.maintenanceEntry.device.code = data.code
      this.maintenanceEntry.open = true
      this.getMaintenanceEntryList()
    },
    // 查询保养项
    getMaintenanceEntryList(){
      this.maintenanceEntry.loading = true;
      this.maintenanceEntry.queryParams.deviceId = this.maintenanceEntry.device.id
      getMaintenanceDeviceEntryPage(this.maintenanceEntry.queryParams).then(response => {
        this.maintenanceEntry.list = response.data.list;
        this.maintenanceEntry.total = response.data.total;
        this.maintenanceEntry.loading = false;
      });
    },
    // 保养项重置
    maintenanceEntryReset(){
      this.maintenanceEntry = {
        title: "保养项",
        open: false,
        device: {
          id: "",
          name: "",
          code: ""
        },
        loading: true,
        list: [],
        total: 0,
        queryParams: {
          pageNo: 1,
          pageSize: 5,
          pageSizes: [5, 10, 20],
          deviceId: null
        },
      }
    },
    // 保养项取消按钮操作
    maintenanceEntryCancel(){
      this.maintenanceEntry.open = false;
      this.maintenanceEntryReset()
    },
    // 保养项确认按钮操作
    maintenanceSubmitForm(){
      this.maintenanceEntry.open = false;
      this.maintenanceEntryReset()
    },
    // 删除检查项按钮操作
    handleMaintenanceEntryDelete(data){
      const id = data.id
      this.$modal.confirm('是否确认删除保养项目编号为"' + id + '"的数据项?').then(function() {
        return deleteMaintenanceDeviceEntry(id);
      }).then(() => {
        this.maintenanceEntry.queryParams.pageNo = 1
        this.getMaintenanceEntryList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 选择保养项按钮操作
    chooseMaintenanceEntry(){
      this.openMaintenanceSelectEntry = true
    },
    /*巡检计划*/
    // 检查项按钮操作
    handleEntry(data){
      this.entryReset()
      this.entry.device.id = data.id
      this.entry.device.name = data.name
      this.entry.device.code = data.code
      this.entry.open = true
      this.getEntryList()
    },
    // 查询检查项
    getEntryList(){
      this.entry.loading = true;
      this.entry.queryParams.deviceId = this.entry.device.id
      getInspectionDeviceEntryPage(this.entry.queryParams).then(response => {
        this.entry.list = response.data.list;
        this.entry.total = response.data.total;
        this.entry.loading = false;
      });

    },
    // 选择检查项按钮操作
    chooseEntry(){
      this.openSelectEntry = true
    },
    // 删除检查项按钮操作
    handleEntryDelete(data){
      const id = data.id
      this.$modal.confirm('是否确认删除检查项编号为"' + id + '"的数据项?').then(function() {
        return deleteInspectionDeviceEntry(id);
      }).then(() => {
        this.entry.queryParams.pageNo = 1
        this.getEntryList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 检查项确认按钮操作
    entrySubmitForm(){
      this.entry.open = false;
      this.entryReset()
    },
    // 检查项取消按钮操作
    entryCancel(){
      this.entry.open = false;
      this.entryReset()
    },
    // 检查项重置
    entryReset(){
      this.entry = {
        title: "检查项",
        open: false,
        device: {
          id: "",
          name: "",
          code: ""
        },
        loading: true,
        list: [],
        total: 0,
        queryParams: {
          pageNo: 1,
          pageSize: 5,
          pageSizes: [5, 10, 20],
          deviceId: null
        },
      }
    },

    /** cron表达式按钮操作 */
    handleShowCron() {
      this.expression = this.form.cycle;
      this.openCron = true;
    },
    /** 确定后回传值 */
    crontabFill(value) {
      this.form.cycle = value;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getInspectionPlanPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.deviceList = []
      this.tableData = []
      this.form = {
        id: undefined,
        name: undefined,
        status: undefined,
        startDate: undefined,
        endDate: undefined,
        cycle: undefined,
        type: undefined,
        reminderTime: undefined,
        estimatedTime: undefined,
        userId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getInspectionPlan(id).then(response => {
        this.form = response.data;
        this.tableData = response.data.deviceList
        response.data.deviceList.forEach(item=>{
          this.deviceList.push({"id": item.id, "code": item.code, "name": item.name})
        })
        this.open = true;
        this.title = "修改设备计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 结束日期在开始日期之后
        if (this.form.endDate < this.form.startDate){
          this.$modal.msgWarning("结束日期不能在开始日期之前")
          return;
        }
        // 检查巡检设备列表
        if (this.deviceList.length <=0 ){
          this.$modal.msgWarning("巡检设备不能为空")
          return;
        }
        this.form.deviceList = this.deviceList
        // 修改的提交
        if (this.form.id != null) {
          updateInspectionPlan(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createInspectionPlan(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除巡检计划编号为"' + id + '"的数据项?').then(function() {
          return deleteInspectionPlan(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 创建任务按钮操作*/
    handleCreateJob(row){
      this.$modal.confirm('是否进行创建任务操作?').then(function() {
        return createInspectionPlanJob({"planId": row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有巡检计划数据项?').then(() => {
          this.exportLoading = true;
          return exportInspectionPlanExcel(params);
        }).then(response => {
          this.$download.excel(response, '巡检计划.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style lang='scss' scoped>
.widthSet{
  width: 220px;
}
</style>
